/*
* app getToken login
* */
import Taro from '@tarojs/taro';
import {showToast} from "./unite";
import {store} from "../store/store";
import {actionCreator} from "../store/actions";
import {logError} from "./http";
import {request} from './index';

function getMiniAppCode(num = 1, isLoginErr) {
    return new Promise(function(resolve){
        if (num < 1) {
            resolve(null);
        }
        (function getcodeII() {
            if(process.env.TARO_ENV === 'weapp') {
                Taro.login().then(res => {
                    if(res.code){
                        resolve(res.code);
                        console.log(res.code + 'code');
                        if(isLoginErr) {
                            logError({
                                infoText: '微信小程序登录成功',
                                isLoginErr: isLoginErr
                            });
                        }
                    } else {
                        throw res;
                    }
                }).catch(err => {
                    console.log(num, err);
                    if(num > 1) {
                        getcodeII();
                        num--;
                    } else {
                        showToast('登录失败,请重新扫码');
                        logError({
                            ...err,
                            infoText: '微信小程序登录失败',
                            isLoginErr: isLoginErr
                        });
                        resolve(null);
                    }
                });
            }
        })();
    })
}

export function reLogin() {
    Taro.setStorageSync('userToken', null);
    Taro.showLoading({
        title: '登陆中…'
    });
    return new Promise(function(resolve){
        if (process.env.TARO_ENV === 'h5') {
            console.log('H5登录');
            resolve('66d01bcge1t3c1ub36j37cP33b56x9Fe6a8e0ec5f4f');
        } else {
            console.log('小程序登录');
            (function apploginII(num = 2, isLoginErr = false) {
                if (num < 1) return;
                getMiniAppCode(num, isLoginErr).then((weappcode) => {
                    if (weappcode) {
                        console.log(weappcode);
                        request({
                            url: '/MiniPro/weiXinLoginByCode',
                            data: {
                                code: weappcode,
                            }
                        }, false).then(val => {
                            if (val) {
                                if (val.code == 200) {
                                    resolve(val);
                                    store.dispatch(actionCreator.setUserToken(val.data.accessToken))
                                }
                                Taro.hideLoading();
                            }
                        })
                    }
                });
            })(2);
        }
    });
}

export function getAppCode() {
    return new Promise((resolve, reject) => {
        if(process.env.TARO_ENV === 'weapp') {
            Taro.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    if (res.code) {
                        //发起网络请求
                        resolve(res.code);
                    } else {
                        reject('登录失败！' + res.errMsg);
                    }
                }
            })
        } else if(process.env.TARO_ENV === 'alipay') {
            my.getAuthCode({
                scopes: 'auth_base',
                success: (res) => {
                    if (res.authCode) {
                        console.log(res);
                        //发起网络请求
                        resolve(res.authCode);
                    } else {
                        console.log('登录失败！' + res.errMsg);
                        reject('登录失败！' + res.errMsg);
                    }
                },
            });
        } else if(process.env.TARO_ENV === 'swan') {
            console.log('百度小程序登录');
            swan.login({
                success: res => {
                    if (res.code) {
                        resolve(res.code);
                    }
                },
                fail: err => {
                    console.log('login fail', err);
                }
            });
        } else if(process.env.TARO_ENV === 'h5') {
            console.log('H5登录');
            resolve('66d01bcge1t3c1ub36j37cP33b56x9Fe6a8e0ec5f4f');
        }
    })

}
